<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="#">活动管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>活动类型</el-breadcrumb-item>
    </el-breadcrumb>
    <el-button type="success" icon="el-icon-check" id="bntAdd" @click="toAddActivityType" >添加</el-button>
    <br/>
    <br/>
    <br/>
    <el-table
        :data="activityTypes"
        border
        style="width: 100%">
      <el-table-column
          prop="id"
          label="ID"
          width="400">
      </el-table-column>
      <el-table-column
          prop="name"
          label="活动类型"
          width="400">
      </el-table-column>
      <el-table-column
          fixed="right"
          width="350"
          label="操作">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="toActivityTypeUpdate(scope.row.id)">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="open(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="活动类型" :visible.sync="dialogFormVisible" :modal-append-to-body="false">
      <el-form :model="form">
        <el-form-item label="类型名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="activityTypeChange">确 定</el-button>
      </div>

    </el-dialog>

  </div>
</template>

<script>
import {
  activityTypeList,
  activityTypeInsert,
  activityTypeUpdate,
  activityTypeById,
  activityTypeDelete
} from '@/api'
export default {
  name: "ActivityTypeList",
  data() {
    return {
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        name: '',
        id: 0,
      },
      formLabelWidth: '120px',
      activityTypes: [],
    }
  },

  created() {
    activityTypeList().then((result)=>{
      this.activityTypes = result.data;
    })
  },

  methods: {
    toAddActivityType(){
      this.dialogFormVisible = true;
      this.form = {
        name: '',
        id: 0,
      };
    },

    activityTypeChange(){
      this.dialogFormVisible = false;
      if (this.form.id === 0){
        let activityType = {name: this.form.name};
        activityTypeInsert(activityType).then(()=>{
          activityTypeList().then((result)=>{
            this.activityTypes = result.data;
          })
        });
      }else {
        let activityType = {id: this.form.id, name: this.form.name};
        activityTypeUpdate(activityType).then(()=>{
          activityTypeList().then((result)=>{
            this.activityTypes = result.data;
          })
        });
      }
    },

    toActivityTypeUpdate(id){
      this.dialogFormVisible = true;
      let activityType = {id: id};
      activityTypeById(activityType).then((result)=>{
        this.form = result.data;
      })
    },

    open(id) {
      this.$confirm('此操作将永久删除该类型, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let activityType = {
          id: id,
        }
        activityTypeDelete(activityType).then(()=>{
          activityTypeList().then(result => {
            this.activityTypes = result.data;
          })
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
  }
}
</script>

<style scoped>
#bntAdd{
  float: right;
}
</style>
